// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.network.packets.hit;

import java.nio.ByteBuffer;
import zombie.characters.IsoGameCharacter;
import zombie.characters.IsoZombie;
import zombie.core.network.ByteBufferWriter;
import zombie.core.raknet.UdpConnection;
import zombie.network.GameServer;
import zombie.network.packets.INetworkPacket;

public class Bite implements INetworkPacket {
    protected short flags;
    protected float hitDirection;

    public void set(IsoZombie wielder) {
        this.flags = 0;
        this.flags = (short)(this.flags | (short)(wielder.getEatBodyTarget() != null ? 1 : 0));
        this.flags = (short)(this.flags | (short)(wielder.getVariableBoolean("AttackDidDamage") ? 2 : 0));
        this.flags = (short)(this.flags | (short)("BiteDefended".equals(wielder.getHitReaction()) ? 4 : 0));
        this.flags = (short)(this.flags | (short)(wielder.scratch ? 8 : 0));
        this.flags = (short)(this.flags | (short)(wielder.laceration ? 16 : 0));
        this.hitDirection = wielder.getHitDir().getDirection();
    }

    @Override
    public void parse(ByteBuffer b, UdpConnection connection) {
        this.flags = b.getShort();
        this.hitDirection = b.getFloat();
    }

    @Override
    public void write(ByteBufferWriter b) {
        b.putShort(this.flags);
        b.putFloat(this.hitDirection);
    }

    @Override
    public String getDescription() {
        return "\n\tBite [ eatBodyTarget="
            + ((this.flags & 1) != 0)
            + " | attackDidDamage="
            + ((this.flags & 2) != 0)
            + " | biteDefended="
            + ((this.flags & 4) != 0)
            + " | scratch="
            + ((this.flags & 8) != 0)
            + " | laceration="
            + ((this.flags & 16) != 0)
            + " | hitDirection="
            + this.hitDirection
            + " ]";
    }

    void process(IsoZombie isoZombie, IsoGameCharacter isoGameCharacter) {
        if ((this.flags & 4) == 0) {
            isoGameCharacter.setAttackedBy(isoZombie);
            if ((this.flags & 1) != 0 || isoGameCharacter.isDead()) {
                isoZombie.setEatBodyTarget(isoGameCharacter, true);
                isoZombie.setTarget(null);
            }

            if (isoGameCharacter.isAsleep()) {
                if (GameServer.bServer) {
                    isoGameCharacter.sendObjectChange("wakeUp");
                } else {
                    isoGameCharacter.forceAwake();
                }
            }

            if ((this.flags & 2) != 0) {
                isoGameCharacter.reportEvent("washit");
                isoGameCharacter.setVariable("hitpvp", false);
            }

            isoZombie.scratch = (this.flags & 8) != 0;
            isoZombie.laceration = (this.flags & 8) != 0;
        }

        isoZombie.getHitDir().setLengthAndDirection(this.hitDirection, 1.0F);
    }
}
